<template>
    <div>

        <i-table  border :columns="columnsTable" :data="dataList">
            <template slot-scope="{ row }" slot="action">
                <div class="action-button">
                    <Button v-if="row.dept_audit_status==='draft'" type="primary" size="small" style="margin-right: 5px" @click="openModal(row.id,true,'提报', row.dept_audit_status)">提报</Button>
                    <Button v-if="row.dept_audit_status==='commit'" type="primary" size="small" style="margin-right: 5px" @click="openModal(row.id,true,'审批', row.dept_audit_status)">审批</Button>
                    <Button v-if="row.dept_audit_status==='audited'&&row.leader_audit_status==='unaudited'" type="primary" size="small" style="margin-right: 5px" @click="openModal(row.id,true,'审批', row.dept_audit_status)">审批</Button>
                    <Button v-if="row.dept_audit_status==='commit'" type="error" size="small" style="margin-right: 5px" @click="openModal(row.id,false,'驳回', row.dept_audit_status)">驳回</Button>
                    <Button type="success" size="small" @click="toDetail(row.imp_date)">详情</Button>
                </div>
            </template>
        </i-table>

        <Page :total="pageInfo.total" :current="pageInfo.pageNo" @on-change="changePage"></Page>
        <Modal
                v-model="modal"
                title="确认操作"
                width="20"
                @on-ok="modalSubmit">
            <div style="text-align:center">
                <p>是否确认{{modalContent}}!</p>
            </div>
        </Modal>
    </div>
</template>

<script>
import { findPageList, updateDeptCommitStatus, updateDeptRejectStatus, updateDeptAuditStatus, updateLeaderAuditStatus } from '@/api/outbreak-management/city-operation/environmental-improvement/recycling-bin-settings'
// 回收桶提报
export default {
  data () {
    return {
      modal: false,
      modalContent: '',
      isReport: false,
      reportStatus: '',
      id: '',
      formCustom: {
        type: '',
        amount: ''
      },
      columnsTable: [
        {
          title: '上报日期',
          minWidth: 120,
          key: 'imp_date',
          render: (h, params) => {
            let date = this.$common.moment(params.row.imp_date).format('YYYY-MM-DD')
            return h('div', date)
          }
        },
        {
          title: '类型',
          minWidth: 120,
          key: 'source_type',
          render: (h, params) => {
            let source_type = ''
            if (params.row.source_type === '0') {
              source_type = '公共场所商业街区'
            } else if (params.row.source_type === '1') {
              source_type = '机关事业单位'
            } else if (params.row.source_type === '2') {
              source_type = '居民小区'
            } else if (params.row.source_type === '3') {
              source_type = '农村'
            }

            return h('div', source_type)
          }
        },
        {
          title: '数量',
          minWidth: 120,
          key: 'quantity'
        },
        {
          title: '提报预警',
          minWidth: 120,
          key: 'warning_status',
          render: (h, params) => {
            switch (params.row.warning_status) {
              case 'normal':
                return h('span', { style: { color: '#2db7f5' } }, '已提报')
              case 'overtime' :
                return h('span', { style: { color: '#ed4014' } }, '超时未提报')
              case 'notset':
                return h('span', { style: { color: '#ff9900' } }, '预警未设置')
              case 'ready':
                return h('span', { style: { color: '#19be6b' } }, '待提报')
            }
          }
        },
        {
          title: '审核状态',
          minWidth: 120,
          key: 'dept_audit_status',
          render: (h, params) => {
            let date = '未知状态'
            let dept_audit_status = params.row.dept_audit_status
            let leader_audit_status = params.row.leader_audit_status
            if (dept_audit_status === 'draft') {
              date = '未提报'
            } else if (dept_audit_status === 'commit') {
              date = '部门已审核'
            } else if (dept_audit_status === 'audited' && leader_audit_status === 'unaudited') {
              date = '分项已审核'
            } else if (dept_audit_status === 'audited' && leader_audit_status === 'audited') {
              date = '区已审核'
            }
            return h('div', date)
          }
        },
        {
          title: '操作',
          slot: 'action',
          fixed: 'right',
          minWidth: 180,
          align: 'center'
        }
      ],
      dataList: [],
      pageInfo: {
        pageNo: 1,
        pageSize: 10,
        total: 0
      }
    }
  },
  methods: {
    toDetail (imp_date) {
      this.$router.push({ name: 'recycling-bin-settings', query: { imp_date: this.$common.moment(imp_date).format('YYYY-MM-DD'), showDetail: true } })
    },
    // 打开弹窗
    openModal (id, isReport, modalContent, reportStatus) {
      if (id) {
        this.isReport = isReport
        this.modalContent = modalContent
        this.reportStatus = reportStatus
        this.modal = true
        this.id = id
      }
    },
    modalSubmit () {
      if (this.isReport) {
        this.reportData()
      } else {
        this.rejectData()
      }
    },
    rejectData () {
      updateDeptRejectStatus({ id: this.id }).then(({ data }) => {
        let { errcode } = data
        if (errcode === 0) {
          this.$Message.success(this.modalContent + '成功!')
          // 清空数据
          this.id = ''
          this.getTableData()
        } else {
          this.$Message.warning(this.modalContent + '失败!')
        }
      })
    },
    // 提报数据
    reportData () {
      if (this.reportStatus === 'commit') {
        updateDeptAuditStatus({ id: this.id }).then(({ data }) => {
          let { errcode } = data
          if (errcode === 0) {
            this.$Message.success(this.modalContent + '成功!')
            // 清空数据
            this.id = ''
            this.getTableData()
          } else {
            this.$Message.warning(this.modalContent + '失败!')
          }
        })
      } else if (this.reportStatus === 'audited') {
        updateLeaderAuditStatus({ id: this.id }).then(({ data }) => {
          let { errcode } = data
          if (errcode === 0) {
            this.$Message.success(this.modalContent + '成功!')
            // 清空数据
            this.id = ''
            this.getTableData()
          } else {
            this.$Message.warning(this.modalContent + '失败!')
          }
        })
      } else if (this.reportStatus === 'draft') {
        updateDeptCommitStatus({ id: this.id }).then(({ data }) => {
          let { errcode } = data
          if (errcode === 0) {
            this.$Message.success(this.modalContent + '成功!')
            // 清空数据
            this.id = ''
            this.getTableData()
          } else {
            this.$Message.warning(this.modalContent + '失败!')
          }
        })
      }
    },
    changePage (index) {
      this.pageInfo.pageNo = index
      this.getTableData()
    },
    getTableData () {
      findPageList(this.pageInfo).then(({ data }) => {
        let { data: res } = data
        this.dataList = res.results
        this.pageInfo.total = res.totalRecord
      })
    }
  },
  created () {
    this.getTableData()
  }
}
</script>

<style scoped>

</style>

<style>
    .submit-button{
        display: flex;
        justify-content: flex-end;
        margin: 10px 0 ;
    }
</style>
